package InterfaceStaff;

import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class FormPresensi extends javax.swing.JFrame {

    private void MatikanTombol() {
        SimpanButton.setEnabled(false);
        DeleteButton.setEnabled(false);

    }

    public FormPresensi() {
        try {
            initComponents();
            List<Mahasiswa> kategori = (List<Mahasiswa>) Presensi.getKoneksi().getAll();
            PresensiModel model = new PresensiModel(kategori);
            TKategori.setModel(model);
        } catch (Exception e) {
            Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, e);
        }
        getComboboxMakul();
        getCombobox();
        setTitle("FORM PRESENSI");
        setResizable(false);
        NIM.requestFocus();
        MatikanTombol();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPopupMenu2 = new javax.swing.JPopupMenu();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem2 = new javax.swing.JCheckBoxMenuItem();
        popupMenu1 = new java.awt.PopupMenu();
        jLabel3 = new javax.swing.JLabel();
        namaMhs = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TKategori = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        makul = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        pengampu = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        NIM = new javax.swing.JTextField();
        SimpanButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        Batal = new javax.swing.JButton();
        Cari = new javax.swing.JButton();
        KeluarButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");

        jCheckBoxMenuItem2.setSelected(true);
        jCheckBoxMenuItem2.setText("jCheckBoxMenuItem2");

        popupMenu1.setLabel("popupMenu1");

        jLabel3.setText("Nama Kategori");

        namaMhs.setToolTipText("Isikan Nama Kategori");
        namaMhs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                namaMhsActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(102, 255, 153));

        jPanel3.setBackground(new java.awt.Color(204, 204, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        TKategori.setBackground(new java.awt.Color(204, 204, 255));
        TKategori.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        TKategori.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "NIM", "Nama"
            }
        ));
        TKategori.setGridColor(new java.awt.Color(0, 0, 0));
        TKategori.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TKategoriMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TKategori);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel4.setBackground(new java.awt.Color(204, 204, 255));
        jPanel4.setForeground(new java.awt.Color(255, 255, 255));

        jLabel6.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel6.setText("Mata Kuliah");

        makul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                makulActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel7.setText("Pengampu");

        jLabel2.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel2.setText("NIM");

        NIM.setToolTipText("Isikan Kode Kategori");
        NIM.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                NIMMouseClicked(evt);
            }
        });
        NIM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NIMActionPerformed(evt);
            }
        });
        NIM.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                NIMKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                NIMKeyTyped(evt);
            }
        });

        SimpanButton.setMnemonic('S');
        SimpanButton.setText("Simpan");
        SimpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SimpanButtonActionPerformed(evt);
            }
        });

        DeleteButton.setMnemonic('D');
        DeleteButton.setText("Delete");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        Batal.setMnemonic('B');
        Batal.setText("Batal");
        Batal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BatalActionPerformed(evt);
            }
        });

        Cari.setText("List");
        Cari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CariActionPerformed(evt);
            }
        });

        KeluarButton.setMnemonic('K');
        KeluarButton.setText("Keluar");
        KeluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeluarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel2)
                    .addComponent(Batal, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(95, 95, 95)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(makul, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(pengampu, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(NIM, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 305, Short.MAX_VALUE)
                        .addComponent(Cari)
                        .addGap(73, 73, 73))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(SimpanButton)
                        .addGap(18, 18, 18)
                        .addComponent(DeleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(KeluarButton)
                        .addContainerGap())))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(makul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Cari))
                .addGap(7, 7, 7)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(pengampu, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(NIM, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Batal)
                    .addComponent(SimpanButton)
                    .addComponent(DeleteButton)
                    .addComponent(KeluarButton))
                .addGap(11, 11, 11))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SimpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SimpanButtonActionPerformed

        TabelPresensi tp = new TabelPresensi();
        String txtCombo = (String) pengampu.getSelectedItem();
        Pengampu pe = new Pengampu();
        pe.setNama_pengampu(txtCombo);

        String txtCombo2 = (String) makul.getSelectedItem();
        MataKuliah makul2 = new MataKuliah();
        makul2.setMakul(txtCombo2);

        Mahasiswa k = new Mahasiswa();
        String no = NIM.getText();

        k.setNIM(NIM.getText());
        tp.setNim(k);
        tp.setId_pengampu(pe);
        tp.setId_matakuliah(makul2);

        try {
            Presensi kategorikontrol;
            kategorikontrol = Presensi.getKoneksi();
            kategorikontrol.tambah(tp);
            JOptionPane.showMessageDialog(this, "Data Masuk");
            CariActionPerformed(evt);
//            refresh();
        } catch (SQLException ex) {
            Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Data Tidak Masuk");
        }

    }//GEN-LAST:event_SimpanButtonActionPerformed

    private void KeluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeluarButtonActionPerformed
        dispose();
    }//GEN-LAST:event_KeluarButtonActionPerformed

    private void namaMhsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_namaMhsActionPerformed
        if (!NIM.getText().equals("")) {
            SimpanButton.setEnabled(true);
            DeleteButton.setEnabled(true);
        }
    }//GEN-LAST:event_namaMhsActionPerformed

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
        Mahasiswa k = new Mahasiswa();
        String id = NIM.getText();

        k.setNIM(id);

        Mahasiswa kat = new Mahasiswa();
        kat.setNIM(id);

        try {
            Presensi kategorikontrol;
            kategorikontrol = Presensi.getKoneksi();
            kategorikontrol.delete(kat);
            JOptionPane.showMessageDialog(this, "Data telah dihapus");
            CariActionPerformed(evt);
        } catch (SQLException ex) {
            Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Data Kategori Belum Dihapus");
        }
        SimpanButton.setText("Simpan");
        NIM.setEnabled(true);
        NIM.setText("");
        namaMhs.setText("");
        MatikanTombol();
        refresh();
    }//GEN-LAST:event_DeleteButtonActionPerformed

    private void NIMKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NIMKeyTyped
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            namaMhs.requestFocus();
        }

    }//GEN-LAST:event_NIMKeyTyped

    private void NIMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NIMActionPerformed
        if (NIM.getText().equalsIgnoreCase("")) {
            SimpanButton.setEnabled(false);
        } else {
            SimpanButton.setEnabled(true);

        }
    }//GEN-LAST:event_NIMActionPerformed

    private void TKategoriMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TKategoriMouseClicked
        int row = TKategori.getSelectedRow();
        String kodeKategori = TKategori.getValueAt(row, 0).toString();
        String namaKategori = TKategori.getValueAt(row, 1).toString();
        NIM.setText(kodeKategori);
        namaMhs.setText(namaKategori);

        NIM.setEnabled(false);
//        SimpanButton.setEnabled(true);
        DeleteButton.setEnabled(true);
//        SimpanButton.setText("Update");
    }//GEN-LAST:event_TKategoriMouseClicked

    private void NIMMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_NIMMouseClicked
        SimpanButton.setText("Simpan");
        MatikanTombol();
    }//GEN-LAST:event_NIMMouseClicked

    private void BatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BatalActionPerformed
        SimpanButton.setText("Simpan");
        NIM.setEnabled(true);
        NIM.setText("");
        namaMhs.setText("");
        MatikanTombol();

    }//GEN-LAST:event_BatalActionPerformed

    private void NIMKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NIMKeyReleased
        if (NIM.getText().equalsIgnoreCase("")) {
            SimpanButton.setEnabled(false);
            DeleteButton.setEnabled(false);
        } else {
            SimpanButton.setEnabled(true);
            DeleteButton.setEnabled(true);
        }
    }//GEN-LAST:event_NIMKeyReleased

    private void makulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_makulActionPerformed
        // TODO add your handling code here:
//        String pengampu
    }//GEN-LAST:event_makulActionPerformed

    private void CariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CariActionPerformed
//        if (Cari.getText().equals("Cari")) {
            String txtCombo = (String) makul.getSelectedItem();
            MataKuliah mk = new MataKuliah();
            mk.setMakul(txtCombo);

            String txtCombo2 = (String) pengampu.getSelectedItem();
            Pengampu p = new Pengampu();
            p.setNama_pengampu(txtCombo2);

            TabelPresensi tp = new TabelPresensi();
            tp.setId_pengampu(p);
            tp.setId_matakuliah(mk);

            try {
                List<Mahasiswa> kategori = (List<Mahasiswa>) Presensi.getKoneksi().getAllCari(tp);
                PresensiModel model = new PresensiModel(kategori);
                TKategori.setModel(model);           
        } catch (SQLException ex) {
            Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, ex);
        }


//        }
    }//GEN-LAST:event_CariActionPerformed

    private void refresh() {
        try {
            List<Mahasiswa> kategori = (List<Mahasiswa>) Presensi.getKoneksi().getAll();
            PresensiModel model = new PresensiModel(kategori);
            TKategori.setModel(model);
        } catch (Exception e) {
            Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(new MetalLookAndFeel());
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, ex);
                }
                new FormPresensi().setVisible(true);
            }
        });
    }

    public void getCombobox() {
        try {
            List<Pengampu> kategoris = Presensi.getKoneksi().getAllPengampu();
            for (Pengampu k : kategoris) {
                pengampu.addItem(k.getNama_pengampu());
            }
        } catch (Exception e) {
            Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, e);
        }

    }

    public void getComboboxMakul() {
        try {
            List<MataKuliah> kategoriss = Presensi.getKoneksi().getAllMakul();
            for (MataKuliah k : kategoriss) {
                makul.addItem(k.getMakul());
            }
        } catch (Exception e) {
            Logger.getLogger(FormPresensi.class.getName()).log(Level.SEVERE, null, e);
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Batal;
    private javax.swing.JButton Cari;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JButton KeluarButton;
    private javax.swing.JTextField NIM;
    private javax.swing.JButton SimpanButton;
    private javax.swing.JTable TKategori;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox makul;
    private javax.swing.JTextField namaMhs;
    private javax.swing.JComboBox pengampu;
    private java.awt.PopupMenu popupMenu1;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the makul
     */
    public String getMakul() {
        String matakuliah = (String) makul.getSelectedItem();
        return matakuliah;
    }

    public String getPengampu() {
        String pengampu2 = (String) pengampu.getSelectedItem();
        return pengampu2;

    }
}
